<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>新增页面</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="">
			<div class="mainBox">
				<div class="main-container">

					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">编码</label>
						<div class="layui-input-inline">
							<input type="text" name="code" placeholder="" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">名称</label>
						<div class="layui-input-inline">
							<input type="text" name="name" placeholder="" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">上级</label>
						<div class="layui-input-inline">
<!--							<input type="text" name="parentid" placeholder="" class="layui-input">-->
							<select id="parentSelect" lay-filter="province" name="parentid" lay-search>
								<option value="">请选择</option>
							</select>
						</div>
					</div>

					<div class="layui-form-item layui-inline layui-hide">
						<label class="layui-form-label">位置</label>
						<div class="layui-input-inline">
							<input type="text" name="pos" placeholder="" class="layui-input">
						</div>
					</div>

				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="user-save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery','upload'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				var upload = layui.upload;
				$.ajaxSetup({
					headers:{
						'Authorization':window.sessionStorage.getItem("token")
					}
				})


				// $.ajax({
				// 	url: window.BASEPATH+'city/selectById/0',
				// 	type: 'GET',
				// 	// dataType: 'json',
				// 	success: function(data) {
				// 		// 填充下拉框选项
				// 		alert(data)
				// 		var select = $('select[name="parentid"]');
				// 		for (var i = 0; i < data.length; i++) {
				// 			select.append('<option value="' + data[i] + '">' + data[i] + '</option>');
				// 		}
				// 		form.render('select'); // 刷新select选择框渲染
				// 	},
				// 	error: function() {
				// 		layer.msg('请求失败');
				// 	}
				// });
				//
				// // 监听选择省份事件
				// form.on('select(province)', function(data){
				// 	alert(province)
				// 	var province = data.field.value;
				// 	console.log('选择的省份为：', province);
				// });

				// 发送Ajax请求获取下拉框的数据
				$.ajax({
					url: window.BASEPATH + 'city/selectById/0', // 替换成实际的数据接口URL
					type: 'GET',
					dataType: 'json',
					success: function(response) {
						if (response.code === 0) {
							var data = response.data;

							// 清空下拉框中的选项
							$('#parentSelect').empty();
							// 添加默认的"请选择"选项
							$('#parentSelect').append('<option value="">请选择</option>');
							// 遍历数据，将每个选项添加到下拉框中
							for (var i = 0; i < data.length; i++) {
								var option = new Option(data[i].name, data[i].id); // 修改这里获取name字段作为选项的值和显示文本
								$('#parentSelect').append(option);
							}
							// 手动重新渲染下拉框
							form.render('select');
						} else {
							// 处理返回的错误信息
						}
					},
					error: function() {
						// 处理请求错误的情况
					}
				});


				$("[name='pos']").val("31.82°,117.22°");

				form.on('submit(user-save)', function(data) {
					

					$.ajax({
						url: window.BASEPATH + 'city/add',
						data: data.field,
						type: 'post',
						success: function(result) {
							if (result.code == 0) {
								layer.msg(result.msg, {
									icon: 1,
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
									parent.layui.table.reload("user-table");
								});
							} else {
								layer.msg(result.msg, {
									icon: 2,
									time: 1000
								});
							}
						}
					});

					return false;
				});


				// form.on('submit(user-save)', function(data) {
				//
				//
				// 	$.ajax({
				// 		url: window.BASEPATH+'city/add',
				// 		data: data.field,
				// 		type: 'post',
				// 		success: function(result) {
				// 			if (result.code==0) {
				// 				layer.msg(result.msg, {
				// 					icon: 1,
				// 					time: 1000
				// 				}, function() {
				// 					parent.layer.close(parent.layer.getFrameIndex(window
				// 						.name)); //关闭当前页
				// 					parent.layui.table.reload("user-table");
				// 				});
				// 			} else {
				// 				layer.msg(result.msg, {
				// 					icon: 2,
				// 					time: 1000
				// 				});
				// 			}
				// 		}
				// 	})
				// 	return false;
				// });
				// upload.render({
				// 	elem: '#idcardpicUpload'
				// 	,url: url2+'upload.do' //改成您自己的上传接口
				// 	,auto: true  //选择文件后不自动上传
				// 	,choose: function(obj){
				// 		//预读本地文件示例，不支持ie8
				// 		obj.preview(function(index, file, result){
				// 			$('#preview').attr('src', result); //图片链接（base64）
				// 		});
				// 	}
				// 	,before: function(obj){
				// 		layer.msg('上传中', {icon: 16, time: 0});
				// 	}
				// 	,done: function(res){
				// 		//如果上传失败
				// 		if(res.code > 0){
				// 			return layer.msg('上传失败');
				// 		}
				// 		$("#idcardpic").val(res.data);
				// 		//上传成功的一些操作
				// 		layer.msg("信息上传成功！",{time: 3000});
				// 	}
				// 	,error: function(){
				// 		//演示失败状态，并实现重传
				// 		var demoText = $('#demoText');
				// 		demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
				// 		demoText.find('.demo-reload').on('click', function(){
				// 			uploadInst.upload();
				// 		});
				// 	}
				//
				// });
			});
		</script>
		<script>
		</script>
	</body>
</html>
